﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace RPG
{
    //Displays text, no events
    public class CGUILabel : CGUIObject
    {
        //Private
        private SpriteFont labelFont;
        private string labelText;
        private Color labelColor;
        private Vector2 labelOrigin;
        private CGUIObject.TextAlign labelAlign;

        //Constructor
        //Creates a new GUI Label. The position should be in coordinates relative to the parent-window.
        public CGUILabel(SpriteFont _labelFont, Vector2 _labelPosition, string _labelText)
        {
            labelFont = _labelFont;
            labelColor = Color.White;
            labelText = _labelText;
            position = _labelPosition;
            boundingBox = new BoundingBox(Vector3.Zero, Vector3.Zero);
            SetTextAlign(TextAlign.TOPLEFT);
        }

        //Called on Draw
        public override void Draw(SpriteBatch spriteBatch)
        {
            
            if (parentWindow != null)
                spriteBatch.DrawString(labelFont, labelText, position + parentWindow.windowPosition, labelColor,
                    0f, labelOrigin, 1f, SpriteEffects.None, 0f);
            else
                spriteBatch.DrawString(labelFont, labelText, position + parentWindow.windowPosition, labelColor,
                    0f, labelOrigin, 1f, SpriteEffects.None, 0f);
        }

        //Called on update
        public override void Update(GameTime gameTime, MouseState oldMouseState, MouseState newMouseState)
        {
            //No events at the moment for fonts
        }

        public void SetTextAlign(CGUIObject.TextAlign where)
        {
            labelAlign = where;
            switch (where)
            {
                case CGUIObject.TextAlign.TOPLEFT:
                    labelOrigin = Vector2.Zero;
                    break;
                case CGUIObject.TextAlign.CENTERED:
                    labelOrigin = labelFont.MeasureString(labelText) / 2;
                    break;
                case CGUIObject.TextAlign.TOPRIGHT:
                    labelOrigin = new Vector2(labelFont.MeasureString(labelText).X, 0);
                    break;
                case CGUIObject.TextAlign.BOTTOMLEFT:
                    labelOrigin = new Vector2(0, labelFont.MeasureString(labelText).Y);
                    break;
                case CGUIObject.TextAlign.BOTTOMRIGHT:
                    labelOrigin = labelFont.MeasureString(labelText);
                    break;
                case TextAlign.TOP:
                    labelOrigin = new Vector2(labelFont.MeasureString(labelText).X / 2, 0);
                    break;
                case TextAlign.BOTTOM:
                    labelOrigin = new Vector2(labelFont.MeasureString(labelText).X / 2, labelFont.MeasureString(labelText).Y);
                    break;
                case TextAlign.LEFT:
                    labelOrigin = new Vector2(0, labelFont.MeasureString(labelText).Y / 2);
                    break;
                case TextAlign.RIGHT:
                    labelOrigin = new Vector2(labelFont.MeasureString(labelText).X, labelFont.MeasureString(labelText).Y / 2);
                    break;
                default:
                    break;
            }
        }

        //Sets
        public void SetLabelText(string whichText){ labelText = whichText; SetTextAlign(labelAlign); }
        public void SetLabelColor(Color whichColor){ labelColor = whichColor; }
        public void SetLabelFont(SpriteFont whichFont){ labelFont = whichFont; SetTextAlign(labelAlign); }
    }
}
